package com.example.ai.demo.invoke.tool;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import com.example.ai.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 *文件操作工具
 */
public class FileOperationTool {
    private final String FILE_DIR = FileConstant.FILE_SAVE_DIR +"/file";

    @Tool(description = "Read content from file")
    public String readFile(@ToolParam(description = "Name of the file to write") String fileName,
                           @ToolParam(description = "Content to write to the file") String content) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(content, filePath);
            return "File written successfully" + filePath;
        } catch (IORuntimeException e) {
            return "ERROR wrting to file" + e.getMessage();
        }
    }
        @Tool(description = "Read content from file")
        public String writeFile(@ToolParam(description = "Name of the file to write") String fileName,
                @ToolParam(description = "Content to write to the file") String content) {
            String filePath = FILE_DIR + "/" + fileName;
            try {
                FileUtil.mkdir(FILE_DIR);
                FileUtil.writeUtf8String(content, filePath);
                return "File written successfully" + filePath;
            }catch (IORuntimeException e){
                return "ERROR wrting to file"+e.getMessage();
            }

    }
}

